package com.xyzisme76.application;

import com.xyzisme76.pojo.Course;
import com.xyzisme76.pojo.Sc;
import com.xyzisme76.pojo.Student;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class Statistical {
    private static final int WIDTH =860;
    private static final int HEIGHT =600;
    Statistical(){
        sqlSelectInfo();
    }

    void sqlSelectInfo() {
        JFrame jf = new JFrame("sql");
        jf.setSize(new Dimension(WIDTH,HEIGHT));
        //jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 创建内容面板，使用边界布局
        //JPanel panel = new JPanel(new BorderLayout());
        // 所在位置 居中
        jf.setLocationRelativeTo(null);
        //设置背景图片
        JPanel jp = new JPanel() {
            protected void paintComponent(Graphics g) {
                try {
                    Image bg = ImageIO.read(new File(
                            "src/main/java/com/xyzisme76/application/images/page3.PNG"));
                    g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
        };
        //初始化显示学生信息表，后期会刷新表单

        // 表头（列名）


        java.util.List<Student> students = StudentInfo.studentsGet();
        Iterator var4 = students.iterator();
        int forNut = students.size();
        Object[] columnNames = {"学号", "姓名", "性别", "年龄", "所在系"};
        Object[][] rowData = new Object[forNut][5];// 表格所有行数据
        int j = 0;
        while(var4.hasNext()) {
            Student student = (Student)var4.next();
            rowData[j][0] = student.getSno();
            rowData[j][1] = student.getName();
            rowData[j][2] = student.getSex();
            rowData[j][3] = student.getAge();
            rowData[j][4] = student.getDept();
            j++;
        }
        // 创建 表格模型，指定 所有行数据 和 表头
        TableModel tableModel = new DefaultTableModel(rowData, columnNames);

        // 使用 表格模型 创建 表格
        JTable table = new JTable(tableModel);
        JButton jFail = new JButton("统计不及格人数");
        JButton jSc = new JButton("按班级统计分数段");
        JButton jTittle = new JButton("各科平均分");
        JTextArea information = new JTextArea("信息");

        jp.add(jTittle);
        //jp.add(jStudent);
        jp.add(jFail);
        jp.add(jSc);
        jp.add(information);
        jp.setLayout(null);

        //布局太麻烦了，直接绝对位置
        jFail.setForeground(Color.LIGHT_GRAY);
        jFail.setFont(new Font("微软雅黑", Font.BOLD, 30));
        jTittle.setForeground(Color.DARK_GRAY);
        jTittle.setFont(new Font("微软雅黑", Font.BOLD, 30));
        jSc.setForeground(Color.LIGHT_GRAY);
        jSc.setFont(new Font("微软雅黑", Font.BOLD, 30));

        information.setFont(new Font("微软雅黑", Font.BOLD, 15));
        //将 button 完全透明



        jFail.setBackground(Color.white);
        jFail.setMargin(new Insets(0,0,0,0));//将边框外的上下左右空间设置为0
        jFail.setBorderPainted(false);//不打印边框
        jFail.setFocusPainted(false);//除去焦点的框
        jFail.setContentAreaFilled(false);//除去默认的背景填充
        jFail.addActionListener(e -> {
            information.setText("");
            List<Sc> scList = ScInfo.ScGet();

            Iterator var = scList.iterator();
            while(var.hasNext()) {
                Sc sc = (Sc) var.next();
                if (sc.getGrade() < 60){
                    Student student = (Student) GetInfomation.getSt(sc.getSno());
                    information.append( student.getName() + "       课号: ");
                    information.append( sc.getCno() + "      成绩: " + sc.getGrade() + "\n");

                }
            }
            jFail.setForeground(Color.DARK_GRAY);
            jTittle.setForeground(Color.LIGHT_GRAY);
            jSc.setForeground(Color.LIGHT_GRAY);

        });

        jTittle.setBackground(Color.white);
        jTittle.setMargin(new Insets(0,0,0,0));//将边框外的上下左右空间设置为0
        jTittle.setBorderPainted(false);//不打印边框
        jTittle.setFocusPainted(false);//除去焦点的框
        jTittle.setContentAreaFilled(false);//除去默认的背景填充
        jTittle.addActionListener(e -> {
          

        });


        jSc.setBackground(Color.white);
        jSc.setMargin(new Insets(0,0,0,0));//将边框外的上下左右空间设置为0
        jSc.setBorderPainted(false);//不打印边框
        jSc.setFocusPainted(false);//除去焦点的框
        jSc.setContentAreaFilled(false);//除去默认的背景填充
        jSc.addActionListener(e -> {


            jSc.setForeground(Color.DARK_GRAY);
            jFail.setForeground(Color.LIGHT_GRAY);
            jTittle.setForeground(Color.LIGHT_GRAY);



        });

        jTittle.setBounds(100,10,190,60);
        jFail.setBounds(300,10,250,60);
        jSc.setBounds(600,10,250,60);
        information.setBounds(100,70,300,300);
        jf.setContentPane(jp);


        jf.setLocationRelativeTo(null);
        jf.setVisible(true);

    }
}
